#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

typedef struct btrees
{
	int data;
	int s,e;
	struct btrees *left;
	struct btrees *right;
}stree;

int segfree(stree **head)
{
	if((*head)->left!=NULL) segfree(&(*head)->left);
	if((*head)->right!=NULL) segfree(&(*head)->right);
	free((*head));
	return 0;
}
	

int segquery(stree **head,int s,int e)
{
	int ss,se;
	int min,minr,minl;
	ss=(*head)->s;
	se=(*head)->e;
	if(s==ss&&e==se) return (*head)->data;
	if(s>(ss+se)/2) return segquery(&(*head)->right,s,e);
	if(e<=(ss+se)/2) return segquery(&(*head)->left,s,e);
	minl=segquery(&(*head)->left,s,(ss+se)/2);
	minr=segquery(&(*head)->right,(ss+se)/2+1,e);
	return (minl>minr)?minr:minl;
}

int consegment(stree **head,int s,int e,int *num)
{
	int dl,dr;
	stree *temp;
	temp=(stree *)malloc(sizeof(stree));
	temp->s=s;
	temp->e=e;
	temp->left=NULL;
	temp->right=NULL;
	(*head)=temp;
	if(s==e)
	{
		temp->data=num[s-1];
		return num[s-1];
	}
	dl=consegment(&(temp)->left,s,(s+e)/2,num);
	dr=consegment(&(temp)->right,(s+e)/2+1,e,num);
	temp->data=(dr>dl)?dl:dr;
	return temp->data;
}
	


int main()
{
	int i,j,k;
	int n;
	int si,se;
	int *num;
	stree *head=NULL;
	scanf("%d",&n);
	num=(int *)malloc(n*sizeof(int));
	for(i=0;i<n;i++) scanf("%d",&num[i]);
	consegment(&head,1,n,num);
	printf("hello\n");
	scanf("%d %d",&si,&se);
	printf("segquery =%d\n",segquery(&head,si,se));
	segfree(&head);
	free(num);
	return 0;
}
	



